package com.layblog.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.layblog.common.lang.Result;
import com.layblog.entity.User;
import com.layblog.entity.UserSign;
import com.layblog.mapper.UserSignMapper;
import com.layblog.service.UserService;
import com.layblog.service.UserSignService;
import com.layblog.utils.DateUtils;
import com.layblog.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Random;

/**
 * <p>
 * 用户签到记录服务实现类
 * </p>
 *
 * @author JackLin
 * @since 2022-04-30
 */
@Slf4j
@Service
public class UserSignServiceImpl extends ServiceImpl<UserSignMapper, UserSign> implements UserSignService {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtil redisUtil;
    @Resource
    private UserSignMapper userSignMapper;

    public static void main(String[] args) {
        DateTime yesterday = DateUtil.yesterday();
        System.out.println("yesterday:" + yesterday);

        System.out.println(DateUtils.formatDate(yesterday, "yyyy-MM-dd"));

        int i = (int) (Math.random() * 900) + 100;
        int j = new Random().nextInt(900) + 100;
        System.out.println(i);
        System.out.println(j);
    }

    @Override
    public Result sign(String userId) {
        String date = DateUtils.formatDate();
        //查看今天是否已签到
        if (baseMapper.getSignInThisDate(userId, date) > 0) {
            return Result.fail("用户已签到!");
        } else {
            //开启签到，保存签到记录
            UserSign userSign = new UserSign();
            userSign.setUserId(userId);
            userSign.setSignTime(new Date());
            userSign.setSignDate(DateUtils.formatDate(new Date()));
            userSign.setCreated(new Date());
            this.save(userSign);

            //更新用户连续签到天数
            User user = userService.getById(userId);
            if (user != null) {
                int continueSignDays = user.getContinueSignDays();
                if (continueSignDays > 0) {
                    int yesterdaySignCount = userSignMapper.queryYesterdaySignDetailCount(userId, DateUtils.formatDate(DateUtil.yesterday()));
                    if (yesterdaySignCount > 0) {
                        //如果昨天已签到，更新连续签到数 = continueSignDays+1
                        user.setContinueSignDays(continueSignDays + 1);
                    } else {
                        //昨天为签到，更新连续签到天数为1
                        user.setCommentCount(1);
                    }
                } else {
                    //如果连续签到数等于0
                    user.setCommentCount(1);
                }

                //每次签到随机送100~999三位数积分
                user.setPoint(user.getPoint() + new Random().nextInt(900) + 100);
            }
            userService.updateById(user);
        }

        return Result.succ("签到成功");
    }
}
